package com.qianli.user.domain.respository;

import com.fqgj.common.utils.MD5;
import com.fqgj.xjd.user.dao.UserAccessDao;
import com.fqgj.xjd.user.entity.UserAccessEntity;
import com.qianli.user.domain.model.UserAccessModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * name:用户存储
 * auth:ting.li@qianli-inc.com
 * date:2017/12/28 19:18
 * version: v1.0.0
 **/
@Repository
public class UserAccessRespository implements BaseRespository<UserAccessModel> {

    @Autowired
    private UserAccessDao userAccessDao;

    @Override
    public void load(UserAccessModel model) {
        UserAccessEntity accessEntity = null;
        if (null != model.getUserCode()) {
            accessEntity = userAccessDao.selectUserAccessByUserCode(model.getUserCode());
        } else {
            accessEntity = userAccessDao.selectUserByMobileAndAppCodeAndBiz(model.getMobile(), model.getAppCode(), model.getBiz());
        }

        if (null != accessEntity) {
            model.setId(accessEntity.getId());
            model.setMobile(accessEntity.getMobile());
            model.setCustomerCode(accessEntity.getCustomerCode());
            //model.setBiz(accessEntity.getBiz());
            model.setUserCode(accessEntity.getUserCode());
            model.setAppCode(String.valueOf(accessEntity.getAppCode()));
        }
    }

    @Override
    public void store(UserAccessModel model) {

        UserAccessEntity accessEntity = new UserAccessEntity();
        BeanUtils.copyProperties(model, accessEntity);
        accessEntity.setAppCode(String.valueOf(model.getAppCode()));
        accessEntity.setCreateDt(new Date());
        accessEntity.setMd5Mobile(MD5.md5(model.getMobile()));
        userAccessDao.insert(accessEntity);

    }

    @Override
    public void restore(UserAccessModel model) {
        UserAccessEntity accessEntity = new UserAccessEntity();
        BeanUtils.copyProperties(model, accessEntity);
        accessEntity.setAppCode(String.valueOf(model.getAppCode()));
        userAccessDao.updateByPrimaryKey(accessEntity);
    }
}
